module SHJ (
    input            CLK,
    input            RST,
    input      [1:0] money,  //钱数10为1块钱 01为0.5
    output reg [4:0] sell    //卖出的瓶数
);
  parameter lost_0 = 2'b00;  //没投钱
  parameter lost_1 = 2'b10;  //投了一块钱
  parameter lost_0_5 = 2'b01;  //投了0.5
  parameter ST0 = 5'b10000;  //空闲
  parameter ST1 = 5'b00100;
  parameter ST2 = 5'b01000;
  parameter ST3 = 5'b00001;
  parameter ST4 = 5'b00011;
  reg [4:0] current_state, next_state;  //当前和次态
  always @(*) begin
    sell <= current_state;
  end
  //设定初值
  always @(posedge CLK or negedge RST) begin
    if (!RST) begin
      current_state <= ST0;
    end else begin
      current_state <= next_state;
    end
  end
  always @(current_state or money) begin
    case (current_state)
      //目前的余额
      ST0:
      //0+0.5=0.5
      if (money == lost_0_5) begin
        next_state <= ST1;
      end  //0+1=1
      else if(money==lost_1)begin
        next_state <= ST2;
      end //0+0=0
      else begin
        next_state <= ST0;
      end
      //余额0.5
      ST1:
      //0.5+0.5=1
      if (money == lost_0_5) begin
        next_state <= ST0;
      end  //0.5+1=1.5
      else if(money==lost_1)begin
        next_state <= ST3;
      end//0.5+0=0.5 
      else begin
        next_state <= ST1;
      end
      //余额1
      ST2:
      //1+0.5=1.5
      if (money == lost_0_5) begin
        next_state <= ST3;
      end //1+1=2
      else if(money==lost_1) begin
        next_state <= ST4;
      end//1+0=1
      else begin
        next_state = ST2;
      end
    endcase
  end
endmodule
